
package com.platform.modules.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.EasyPoiUtils;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.entity.MallCardSnEntity;
import com.platform.modules.mall.service.MallCardSnService;
import com.platform.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 卡卷编码Controller
 *
 * @author tech
 * @date 2020-08-04 22:52:36
 */
@RestController
@RequestMapping("mall/cardsn")
public class MallCardSnController extends AbstractController {
    @Autowired
    private MallCardSnService mallCardSnService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("mall:cardsn:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<MallCardSnEntity> list = mallCardSnService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询卡卷编码
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("mall:cardsn:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page<MallCardSnEntity> page = mallCardSnService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param cardId 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{cardId}")
    @RequiresPermissions("mall:cardsn:info")
    public RestResponse info(@PathVariable("cardId") String cardId) {
        MallCardSnEntity mallCardSn = mallCardSnService.getById(cardId);
        String cardSn = mallCardSn.getCardSn();
        mallCardSn.setCardSn(cardSn.substring(0,2)+"****"+cardSn.substring(cardSn.length()-2));
        return RestResponse.success().put("cardsn", mallCardSn);
    }

    /**....0

     * 新增卡卷编码
     *
     *
     * gyg
     *
     * @param mallCardSn mallCardSn
     * @return RestResponse
     */
    @SysLog("新增卡卷编码")
    @RequestMapping("/save")
    @RequiresPermissions("mall:cardsn:save")
    public RestResponse save(@RequestBody MallCardSnEntity mallCardSn) {

        mallCardSnService.add(mallCardSn);

        return RestResponse.success();
    }

    /**
     * 修改卡卷编码
     *
     * @param mallCardSn mallCardSn
     * @return RestResponse
     */
    @SysLog("修改卡卷编码")
    @RequestMapping("/update")
    @RequiresPermissions("mall:cardsn:update")
    public RestResponse update(@RequestBody MallCardSnEntity mallCardSn) {

        mallCardSn.setCardSn(null);
        mallCardSnService.update(mallCardSn);

        return RestResponse.success();
    }

    /**
     * 根据主键删除卡卷编码
     *
     * @param cardIds cardIds
     * @return RestResponse
     */
    @SysLog("删除卡卷编码")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:cardsn:delete")
    public RestResponse delete(@RequestBody String[] cardIds) {
        mallCardSnService.deleteBatch(cardIds);

        return RestResponse.success();
    }

    /**
     * 上传文件
     *
     * @param file file
     * @return RestResponse
     */
    @SysLog("卡券导入")
    @RequestMapping("/upload")
    @RequiresPermissions("mall:cardsn:save")
    public Object upload(@RequestParam(value = "file", required = false) MultipartFile file, String goodsId) throws Exception {
        if (null == file || file.isEmpty()) {
            throw new BusinessException("上传文件不能为空");
        }

        EasyPoiUtils.checkFile(file);
        //上传文件
        Map<String, List<String[]>> map = EasyPoiUtils.readExcel(file.getOriginalFilename(), file.getInputStream());
        Iterator<Map.Entry<String, List<String[]>>> it = map.entrySet().iterator();
        List<String[]> list = new ArrayList();
        while (it.hasNext()) {
            Map.Entry<String, List<String[]>> entry = it.next();
            list = entry.getValue();
            break;
        }
        MallCardSnEntity cardSnEntity;
        List<MallCardSnEntity> cardSnEntityList = new ArrayList(list.size() + 1);
        for (int i = 1, num = list.size(); i < num; i++) {
            if (StringUtils.isNotBlank(list.get(i)[1]) && StringUtils.isNotBlank(list.get(i)[0])) {
                cardSnEntity = new MallCardSnEntity();
                cardSnEntity.setType(Integer.parseInt(list.get(i)[1]));
                cardSnEntity.setCardSn(list.get(i)[0]);
                cardSnEntity.setGoodsId(goodsId);
                try {
                    cardSnEntity.setExpireTime(new Date(list.get(i)[2]));
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new BusinessException("过期时间获取失败！请填写过期时间");
                }

//                if(cardSnEntity.getType() == 98){
//                    try {
//                        cardSnEntity.setExpireTime(new Date(list.get(i)[2]));
//                    }catch (Exception e){
//                        e.printStackTrace();
//                        throw new BusinessException("过期时间获取失败！");
//                    }
//                }
                cardSnEntityList.add(cardSnEntity);
            }
        }

        if (cardSnEntityList.size() > 0) {
            mallCardSnService.saveBatch(cardSnEntityList);
        }

        return RestResponse.success();
    }


    //卡券导出
    @SysLog("卡券导出")
    @PostMapping("/downLoad")
    @RequiresPermissions("mall:cardsn:list")
    public void downLoad(HttpServletResponse response, HttpServletRequest request,
                         @RequestBody Map<String, Object> params) {
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Origin," +
                "Access-Control-Allow-Headers,Content-Type,x-requested-with,content-type,token");
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

        mallCardSnService.cardSnExport(response,params);

    }

}
